package com.property.manage.service.flowingWater;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.property.manage.dao.flowingWater.PropertyDeductionMapper;
import com.property.manage.model.flowingWater.PropertyDeductionEntity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @author lwb
 **/
@Service
@AllArgsConstructor
public class PropertyDeductionService {

    private PropertyDeductionMapper propertyDeductionMapper;

    public void add(Integer villageId, Integer year, Integer month){
        PropertyDeductionEntity entity = new PropertyDeductionEntity();
        entity.setVillageId(villageId);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setState(PropertyDeductionEntity.STATE_DEDUCTING);
        entity.setCreateTime(LocalDateTime.now());

        propertyDeductionMapper.insert(entity);
    }

    public Optional<PropertyDeductionEntity> find(Integer villageId, Integer year, Integer month){
        QueryWrapper<PropertyDeductionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("village_id", villageId);
        wrapper.eq("year", year);
        wrapper.eq("month", month);

        return Optional.ofNullable(propertyDeductionMapper.selectOne(wrapper));
    }

    public void complete(Integer villageId, Integer year, Integer month){
        UpdateWrapper<PropertyDeductionEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("village_id", villageId);
        wrapper.eq("year", year);
        wrapper.eq("month", month);

        wrapper.set("state", PropertyDeductionEntity.STATE_DEDUCTED);

        propertyDeductionMapper.update(null, wrapper);
    }
}
